﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using System.Net.Sockets;
using System.Net;
using System.Diagnostics;
using System.Windows.Forms;

namespace learnCsharpwinform
{
    class TcpConn
    {

      private  int receiveLength;
        private float[] send_data;
      private  float[] receive_data;

       private IPAddress ip;
        private string sendMessage;
        private string receiveMessage;
        private int port;
        private static byte[] result;
        public delegate void InvokeForm(string message);
        public InvokeForm Dele_InvokeForm;
        private Socket clientSocket;
        public TcpConn()
        {
           
            result = new Byte[1024];
        }

        public bool InitConn()
        {

            //初始化发送信息
          receiveLength = 0;
            receive_data = new float[6] { 0, 0, 0, 0, 0, 0 };
            send_data = new float[6] { 1.11f, 2.22f, 3.33f, 4.44f, 5.55f, 6.66f };
            string str1 = "5.55";
            string str2 = "0.55";
            string str3 = "7.55";
            string str4 = "4.55";
            string str5 = "5.55";
            string str6 = "6.55";
            float f1 = float.Parse(str1);
            float f2 = float.Parse(str2);
            float f3 = float.Parse(str3);
            float f4 = float.Parse(str4);
            float f5 = float.Parse(str5);
            float f6 = float.Parse(str6);
          sendMessage = string.Format("{0:f} {1:f} {2:f} {3:f} {4:f} {5:f}", f1, f2, f3, f4, f5, f6);

            //建立连接
            bool ConnIsOK=false;
            ip = IPAddress.Parse("192.168.111.200");
            port = 1025;
           clientSocket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);

            try
            {
                clientSocket.Connect(new IPEndPoint(ip, port));
                Console.WriteLine("连接服务器成功");
                ConnIsOK = true;

            }

            catch
            {
                Debug.WriteLine("连接服务器失败,请按回车键退出");
                Dele_InvokeForm("没找到服务器啊老哥");

            }
            if (ConnIsOK)
            {
                Thread ThreadCom = new Thread(Communication);
                ThreadCom.Start();
            }
            return ConnIsOK;
        }




        private void Communication()
        {
            
                Debug.WriteLine("成功启动监听线程");
            try
            {
                receiveLength = clientSocket.Receive(result);
                receiveMessage = Encoding.ASCII.GetString(result, 0, receiveLength);
                Console.WriteLine("接收服务器消息：{0}", receiveMessage);

                Dele_InvokeForm(receiveMessage);
                // 通过clientSocket发送数据

                for (int i = 0; i < 10; i++)
                {
                    try
                    {
                        Thread.Sleep(2000);

                        //  string sendMessage =  DateTime.Now;
                        clientSocket.Send(Encoding.ASCII.GetBytes(sendMessage));

                        Debug.WriteLine("向服务器发送消息:{0}", sendMessage);
                        Dele_InvokeForm(sendMessage);

                        receiveLength = clientSocket.Receive(result);
                        //    string[] split_data = result.();
                        Debug.WriteLine("接收服务器消息：{0}", Encoding.ASCII.GetString(result, 0, receiveLength));
                    }

                    catch
                    {
                        clientSocket.Shutdown(SocketShutdown.Both);
                        clientSocket.Close();
                        Dele_InvokeForm("接受服务器消息失败");
                        break;
                    }
                }
                Dele_InvokeForm("结束通信");
            }
            catch
            {
                Dele_InvokeForm("呵呵，并没有建立连接");
                Debug.WriteLine("长时间没收到消息");
                Application.DoEvents();
            }

            
            }
        


    }
}
